import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import env from '@/utils/env'

// import { useStorage, StorageType } from '@/utils/storage'
import { useFromRoute } from '@/composables/useFromRoute'

import Layout from '@/layout/index.vue'

// const sessionStorage = useStorage('app', StorageType.SessionStorage)

export const constantRoutes: Array<RouteRecordRaw> = [
  {
    path: '/redirect',
    component: Layout,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect.vue'),
      },
    ],
    meta: { hidden: true },
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login.vue'),
    meta: { hidden: true },
  },
  {
    path: '/register',
    component: () => import('@/views/register.vue'),
    meta: { hidden: true },
  },
  {
    path: '/404',
    component: () => import('@/views/error/404.vue'),
    meta: { hidden: true },
  },
  {
    path: '/401',
    component: () => import('@/views/error/401.vue'),
    meta: { hidden: true },
  },
  {
    path: '/writeOff',
    component: () => import('@/views/open/writeOff/index.vue'),
    meta: { title: '核销验证', hidden: true },
  },
  {
    path: '/coupons/receive',
    component: () => import('@/views/open/coupons/receive/index.vue'),
    meta: { title: '优惠券领取', hidden: true },
  },
  {
    path: '/',
    component: Layout,
    redirect: 'index',
    children: [
      // {
      //   path: '/index',
      //   component: () => import('@/views/test.vue'),
      //   name: 'Index',
      //   meta: { title: '首页', icon: 'dashboard', affix: true, hidden: true },
      // },
    ],
  },
  {
    path: '/user',
    component: Layout,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index.vue'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' },
      },
    ],
    meta: { hidden: true },
  },
]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes: Array<RouteRecordRaw> = [
  {
    path: '/system/user-auth',
    component: Layout,
    meta: {
      hidden: true,
      permissions: ['system:user:edit'],
    },
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole.vue'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' },
      },
    ],
  },
  {
    path: '/system/role-auth',
    component: Layout,
    meta: {
      hidden: true,
      permissions: ['system:role:edit'],
    },
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser.vue'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role' },
      },
    ],
  },
  {
    path: '/system/dict-data',
    component: Layout,
    meta: {
      hidden: true,
      permissions: ['system:dict:list'],
    },
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data.vue'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict' },
      },
    ],
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    meta: {
      hidden: true,
      permissions: ['monitor:job:list'],
    },
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log.vue'),
        name: 'JobLog',
        meta: { title: '调度日志', activeMenu: '/monitor/job' },
      },
    ],
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    meta: {
      hidden: true,
      permissions: ['tool:gen:edit'],
    },
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable.vue'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' },
      },
    ],
  },
]

const router = createRouter({
  history: createWebHistory(env.VITE_BASEPATH),
  routes: constantRoutes,
})

/**
 * 路由拦截
 * 权限验证

 */

// const initstamp = { value: 0 }
router.beforeEach((to, from, next) => {
  // 存储进入的 from 路由
  // 因为vue router4.x 不提供onBeforeRouteEnter勾子，
  // 组件内无法获取 from 路由信息
  const { setFromRoute } = useFromRoute()
  setFromRoute(from)
  // sessionStorage.set('fromRoute', from)
  next()

  NProgress.start()
})

router.afterEach(() => {
  NProgress.done()
})

//

export default router
